package com.tanhua.admin.controller;

import com.itheima.tanhua.domain.UserInfo;
import com.itheima.tanhua.dto.UserFreezeDto;
import com.itheima.tanhua.vo.MovementsVo;
import com.itheima.tanhua.vo.PageResult;
import com.tanhua.admin.service.ManageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @author Lff
 * @date 2022/4/5
 * 用户管理
 */
@RestController
@RequestMapping("/manage")
public class ManageController {
    @Autowired
    private ManageService manageService;


    /**
     * 用户数据翻页
     *
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam("page") Integer page,
                                @RequestParam("pagesize") Integer pagesize) {
        PageResult pr = manageService.findUserInfoPage(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/users/{userID}")
    public ResponseEntity userInfo(@PathVariable("userID") Long userId) {
        UserInfo userInfo = manageService.findUserInfoById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 动态分页
     */
    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "1") Integer page,//页数
                                   @RequestParam(defaultValue = "10") Integer pagesize,//页尺寸
                                   @RequestParam(value = "uid", required = false) Long uid,//用户id
                                   @RequestParam(value = "sortProp", required = false) String sortProp,//排序字段
                                   @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则
                                   @RequestParam(value = "state", required = false) Integer status) {//动态审核状态
        if (!StringUtils.isEmpty(sortProp)) {
            sortProp = sortProp.equals("createDate") ? "created" : sortProp;
        }
        PageResult pr = manageService.findUserMovements(page, pagesize, uid, status, sortProp, sortOrder);
        return ResponseEntity.ok(pr);
    }

    /**
     * 动态详情
     */
    @GetMapping("messages/{id}")
    public ResponseEntity getMovementOne(@PathVariable("id") String movementId) {
        MovementsVo result = manageService.findMovementsInfoById(movementId);
        return ResponseEntity.ok(result);
    }

    /**
     *
     */
    @GetMapping("/messages/comments")
    /**
     * 视频分页
     */
    public ResponseEntity getMovementAllComments(@RequestParam(defaultValue = "1") Integer page,//页数
                                                 @RequestParam(defaultValue = "10") Integer pagesize,//页尺寸
                                                 @RequestParam(value = "messageID", required = true) String publishId) {//用户id
        PageResult pr = manageService.findMovementAllComments(publishId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,//页数
                                 @RequestParam(defaultValue = "10") Integer pagesize,//页尺寸
                                 @RequestParam(value = "uid", required = false) Long uid,//用户id
                                 @RequestParam(value = "sortProp", required = false) String sortProp,//排序字段
                                 @RequestParam(value = "sortOrder", required = false) String sortOrder) {//排序规则
        if (!StringUtils.isEmpty(sortProp)) {
            sortProp = sortProp.equals("createDate") ? "created" : sortProp;
        }
        PageResult pr = manageService.findUserVideos(page, pagesize, uid, sortProp, sortOrder);
        return ResponseEntity.ok(pr);
    }

    /**
     * 用户冻结
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody UserFreezeDto dto) {

        //冻结操作
        Map map = manageService.freezeUser(dto);
        //返回
        return ResponseEntity.ok(map);
    }

    /**
     * 用户解冻
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map map) {

        //冻结操作
        Map resultMap = manageService.unFreezeUser(map);
        //返回
        return ResponseEntity.ok(map);
    }
}
